﻿using RuoVea.ExCrypt.Extensions;
using System;
using System.Security.Cryptography;
using System.Text;

namespace RuoVea.ExCrypt
{
    /// <summary>
    /// SHA1加密
    /// </summary>
    public class SHA1Crypt
    {
        ///// <summary>
        ///// SHA1加密
        ///// </summary>
        ///// <param name="pwd">密钥</param>
        ///// <returns>SHA1加密内容</returns>
        //public static string Encrypt(string pwd)
        //{
        //    SHA1 algorithm = SHA1.Create();
        //    byte[] data = algorithm.ComputeHash(Encoding.UTF8.GetBytes(pwd));
        //    string sh1 = "";
        //    for (int i = 0; i < data.Length; i++)
        //    {
        //        sh1 += data[i].ToString("x2").ToUpperInvariant();
        //    }
        //    return sh1;
        //}

        #region SHA1
        /// <summary>
        /// SHA1加密 SHA1 Encryption
        /// </summary>
        /// <param name="str">The string to be encrypted</param>
        /// <returns>SHA1加密内容</returns>
        public static string Encrypt(string str)
        {
            Check.Argument.IsNotEmpty(str, nameof(str));

            using (SHA1 sha1 = SHA1.Create())
            {
                byte[] bytes_sha1_in = Encoding.UTF8.GetBytes(str);
                byte[] bytes_sha1_out = sha1.ComputeHash(bytes_sha1_in);
                string str_sha1_out = BitConverter.ToString(bytes_sha1_out);
                str_sha1_out = str_sha1_out.Replace("-", "");
                return str_sha1_out;
            }
        }
        #endregion
    }
}
